using System;
using System.Collections;
using System.Collections.Generic;
using OpenCVForUnity.UnityUtils.Helper;
using UnityEngine;
using UnityEngine.UI;

public class RecordController : MonoBehaviour
{
    public WebCamTextureToMatHelper webCamTextureToMatHelper;
    public PoseEstimationRecorder poseEstimationRecorder;
    public GameObject targetObjParent;
    public Transform targetTrans;
    public Transform targetObjTopPos;
    public Transform targetObjDownPos;
    public Image signIcon;
    public Text tipsText;
    public Sprite frontSign;
    public Sprite sideSign;
    
    private bool m_gyroOK;
    public bool gyroOK => m_gyroOK;
    private void Awake()
    {
        if (poseEstimationRecorder == null) poseEstimationRecorder = GetComponentInChildren<PoseEstimationRecorder>();
        if (webCamTextureToMatHelper == null) webCamTextureToMatHelper = GetComponentInChildren<WebCamTextureToMatHelper>();
        
        poseEstimationRecorder.gameObject.SetActive(true);
        targetObjTopPos.gameObject.SetActive(false);
        targetObjDownPos.gameObject.SetActive(false);
        
        Input.gyro.enabled = true;
    }

    private float m_BeginTime;
    private float m_LastPlayAdjustTargetAudioTime;
    public void Begin(Action<Texture2D,Texture2D> onCapture)
    {
        m_BeginTime = Time.time;
        AudioController.instance.PlayBattleSound("请后退至两米处",transform.position);
        
        poseEstimationRecorder.onRecordedEnd = onCapture;
        poseEstimationRecorder.onRecordFrontTexEnd = this.OnRecordFrontTexEnd;
        poseEstimationRecorder.Reset();
        
        tipsText.text = "请正面拍摄";
        signIcon.sprite = frontSign;
    }

    private void OnRecordFrontTexEnd(Texture2D obj)
    {
        tipsText.text = "请侧面拍摄";
        signIcon.sprite = sideSign;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (poseEstimationRecorder.recording)
        {
            if(targetObjParent.activeSelf)
                targetObjParent.SetActive(false);
            m_gyroOK = true;
            return;
            
            if(!targetObjParent.activeSelf)
                targetObjParent.SetActive(true);
            
            // 获取陀螺仪数据
            Quaternion gyroRotation = Input.gyro.attitude;

            // 将陀螺仪数据转换为欧拉角
            Vector3 eulerRotation = gyroRotation.eulerAngles;
            while (eulerRotation.x < 0)
            {
                eulerRotation.x += 360;
            }

            eulerRotation.x %= 360;
            
            Debug.Log($"eulerRotation:{eulerRotation}");
            
            // 检查设备是否垂直摆正
            if (eulerRotation.x > 80 && eulerRotation.x < 100)
            {
                m_gyroOK = true;
                
                //Debug.Log("设备垂直摆正");
                targetTrans.transform.localPosition = Vector3.zero;
                targetTrans.GetComponent<Image>().color = Color.green;
            }
            else
            {
                m_gyroOK = false;
                
                //Debug.Log("设备非垂直摆正");
                float y = Mathf.Lerp(this.targetObjDownPos.localPosition.y, this.targetObjTopPos.localPosition.y,
                    eulerRotation.x / 180f);
                //float x = Mathf.Lerp(this.targetObjDownPos.localPosition.x, this.targetObjTopPos.localPosition.x,
                //    eulerRotation.x / 180f);
                targetTrans.transform.localPosition = new Vector3(0,y,0);
                targetTrans.GetComponent<Image>().color = Color.yellow;

                if (Application.platform != RuntimePlatform.WindowsEditor &&
                    Application.platform != RuntimePlatform.OSXEditor)
                {
                    if (Time.time - m_BeginTime > 9)
                    {
                        if (Time.time - m_LastPlayAdjustTargetAudioTime > 5)
                        {
                            m_LastPlayAdjustTargetAudioTime = Time.time;
                            AudioController.instance.PlayBattleSound("保持准心与十字重叠", transform.position);
                        }
                    }
                }
            }
        }
        else
        {
            if(targetObjParent.activeSelf)
                targetObjParent.SetActive(false);
        }
    }

    public void OnGoback()
    {
        //webCamTextureToMatHelper.Dispose();
    }
}
